/*
 * Toggles the layer between a solid and gradient fill.
 * Written by Richard Z. - richkzad@gmail.com
 *
 * Thanks to Yasuyuki Higuchi and Scott Weichert for tips on how to reproduce
 * the functionality by creating an action in Photoshop CS3 and then running
 * it in Photoshop CS4
 * http://www.adobeforums.com/webx?128@@.59b6e5bb
 *
 * Thanks to xbytor for writing a script that would convert an action into
 * JavaScript to help understand what was really going on.
 * http://ps-scripts.cvs.sourceforge.net/*checkout*/ps-scripts/xtools/apps/ActionToJavascript.jsx
 */

var changeFill = new ActionDescriptor(),
    layerRef =   new ActionReference();

layerRef.putEnumerated(
    stringIDToTypeID('contentLayer'),
    charIDToTypeID('Ordn'),
    charIDToTypeID('Trgt')
);

changeFill.putReference(
    charIDToTypeID('null'),
    layerRef
);

try {

    var layerKind = typeIDToStringID(executeActionGet(layerRef).getList(charIDToTypeID('Adjs')).getObjectType(0));

    changeFill.putClass(
        charIDToTypeID('T   '),
        stringIDToTypeID(
            'gradientLayer' == layerKind ? 'solidColorLayer' : 'gradientLayer'
        )
    );

    executeAction(charIDToTypeID('setd'), changeFill, DialogModes.ALL);

} catch (e) { }
